package cn.tedu.csmall.stock.webapi.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    //创建一个JobDetail，封装Job实现类，然后交给Spring管理
    @Bean
    public JobDetail showTime(){
        System.out.println("showTime方法运行了");
        return JobBuilder.newJob(QuartzJob.class)
                .withIdentity("dateTime")//给jobDetail起名字
                .storeDurably()//即使没有Trigger绑定当前JobDetail，也不会被删除
                .build();
    }

    @Bean
    public Trigger showTimeTrigger(){
        System.out.println("showTimeTrigger方法运行了");
        //定义Cron表达式
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0/10 * * 2 8 ?");
        return TriggerBuilder.newTrigger()
                //绑定到要运行的JobDetail对象
                .forJob(showTime())
                .withIdentity("dateTimeTrigger")
                .withSchedule(cron)
                .build();
    }
}
